package com.xinchuang.mapper.targetSystem;

import com.xinchuang.entity.targetSystem.Zone;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @ClassName ZoneMapper
 * @Description: TODO(行政区域数据库操作)
 * @Author zhangxuan
 * @Date 2022-05-12 17:31
 * @Verion V1.0
 */
public interface ZoneMapper {

    @Select(value = "SELECT id,adcode,parent,name,zoom,level,children_num,lng,lat,sort,acroutes FROM zone WHERE name LIKE '%${name}%' LIMIT 1")
    Zone getZoneByName(@Param("name") String name);

    @Select(value = "SELECT id,adcode,parent,name,zoom,level,children_num,lng,lat,sort,acroutes FROM zone WHERE adcode = #{adcode} LIMIT 1")
    Zone getZoneByAdcode(@Param("adcode") String adcode);

    @Select(value = "SELECT id,adcode,parent,name,zoom,level,children_num,lng,lat,sort,acroutes FROM zone WHERE parent = #{parent}")
    List<Zone> getZoneByParent(@Param("parent") String parent);

    @Select(value = {
            "<script>",
            "SELECT id,adcode,parent,name,zoom,level,children_num,lng,lat,sort,acroutes FROM zone WHERE 1=1 ",
            "<if test = 'level != null'>",
            "<foreach collection='level' item='item' index='index' open='AND level IN (' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</if>",
            "</script>"
    })
    List<Zone> getZoneAll(@Param("level") List<String> level);

    @Insert(value = {
            "INSERT INTO sign_icon (id,icon) VALUES (#{id},#{icon})",
            "ON DUPLICATE KEY UPDATE id = #{id},icon = #{icon}"
    })
    int addSign(@Param("id") String id, @Param("icon") String name);

    @Select(value = {
            "SELECT COUNT(1) FROM sign_icon WHERE icon = #{icon}"
    })
    int getSign(@Param("icon") String name);

    @Select(value = "SELECT icon FROM sign_icon WHERE id = #{id}")
    String getSignName(@Param("id") String id);

    @Insert(value = {
            "INSERT OR REPLACE INTO type_else (id,radius) VALUES (#{id},#{radius})"
    })
    int addTypeElse(@Param("id") String id, @Param("radius") Double radius);

    @Select(value = {
            "SELECT radius FROM type_else WHERE id = #{id}"
    })
    String getTypeElse(@Param("id") String typeId);

    @Select(value = "SELECT tt.type_id AS id,si.icon FROM target_type tt LEFT JOIN sign_icon si ON tt.type_id = si.id WHERE level != 0")
    List<Map<String, String>> getInitTypeSign();

}
